<?php

require dirname(__FILE__).'/../config.php';

$root_dir = '../';

include WP_VERSION_PATH . '/header.php';

echo '<h2>WP Version Installer</h2>'."\n\n";

if( (false === USE_SQLITE) && (false === USE_MYSQL) )
{
	if( true === wp_version_install_serialized())
		echo '<p><strong>Done!</strong></p>';
	else
		echo '<p>Failed!</p>';
}
else if( (true === USE_SQLITE) && (false === USE_MYSQL) )
{
	if( true === wp_version_install_sqlite())
		echo '<p><strong>Done!</strong></p>';
	else
		echo '<p>Failed!</p>';
}

include WP_VERSION_PATH . '/footer.php';


function wp_version_install_serialized()
{
	if( file_exists(WP_VERSION_STORAGE.'/save.txt') )
	{
		unlink( WP_VERSION_STORAGE.'/save.txt' );
	}

	file_put_contents(WP_VERSION_STORAGE.'/save.txt', '');

	return true;
}

function wp_version_install_sqlite()
{
	$pdo = new PDO('sqlite:'.SQLITE_PATH);
	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	try {
		$pdo->query("CREATE TABLE IF NOT EXISTS wp_version_info ( 
			ID INTEGER PRIMARY KEY AUTOINCREMENT,
			info_type INTEGER(9),
			info_name TEXT(255),
			info_version TEXT(20),
			info_definition TEXT(1000)
		)");
		$pdo->query("CREATE INDEX IF NOT EXISTS info_type ON wp_version_info (info_type)");
		$pdo->query("CREATE INDEX IF NOT EXISTS info_name ON wp_version_info (info_name)");
	} catch(PDOException $e) {
		echo $e->getMessage();
		return false;
	}

	try {
		$pdo->query("CREATE TABLE IF NOT EXISTS wp_version_hook_info ( 
			ID INTEGER PRIMARY KEY AUTOINCREMENT,
			hook_type TEXT(20),
			hook_name TEXT(255),
			hook_definition TEXT(1000),
			hook_file TEXT(512),
			hook_version TEXT(20)
		)");
		$pdo->query("CREATE INDEX IF NOT EXISTS hook_type ON wp_version_hook_info (hook_type)");
		$pdo->query("CREATE INDEX IF NOT EXISTS hook_name ON wp_version_hook_info (hook_name)");
		$pdo->query("CREATE INDEX IF NOT EXISTS hook_version ON wp_version_hook_info (hook_version)");
	} catch(PDOException $e) {
		echo $e->getMessage();
		return false;
	}

	try {
		$pdo->query("CREATE TABLE IF NOT EXISTS wp_types ( 
			ID INTEGER PRIMARY KEY AUTOINCREMENT,
			info_type INTEGER(9),
			info_name TEXT(255),
			version_0_71 INTEGER(1),
			version_1_0_0 INTEGER(1),
			version_1_0_1 INTEGER(1),
			version_1_0_2 INTEGER(1),
			version_1_2_0 INTEGER(1),
			version_1_2_1 INTEGER(1),
			version_1_2_2 INTEGER(1),
			version_1_5_0 INTEGER(1),
			version_1_5_1 INTEGER(1),
			version_1_5_2 INTEGER(1),
			version_2_0_0 INTEGER(1),
			version_2_0_1 INTEGER(1),
			version_2_0_4 INTEGER(1),
			version_2_0_5 INTEGER(1),
			version_2_0_6 INTEGER(1),
			version_2_0_7 INTEGER(1),
			version_2_0_8 INTEGER(1),
			version_2_0_9 INTEGER(1),
			version_2_0_10 INTEGER(1),
			version_2_0_11 INTEGER(1),
			version_2_1_0 INTEGER(1),
			version_2_1_1 INTEGER(1),
			version_2_1_2 INTEGER(1),
			version_2_1_3 INTEGER(1),
			version_2_2_0 INTEGER(1),
			version_2_2_1 INTEGER(1),
			version_2_2_2 INTEGER(1),
			version_2_2_3 INTEGER(1),
			version_2_3_0 INTEGER(1),
			version_2_3_1 INTEGER(1),
			version_2_3_2 INTEGER(1),
			version_2_3_3 INTEGER(1),
			version_2_5_0 INTEGER(1),
			version_2_5_1 INTEGER(1),
			version_2_6_0 INTEGER(1)
		)");
		$pdo->query("CREATE UNIQUE INDEX IF NOT EXISTS unique_name ON wp_types (info_type, info_name)");
		$pdo->query("CREATE INDEX IF NOT EXISTS info_name ON wp_types (info_name)");
	} catch(PDOException $e) {
		echo $e->getMessage();
		return false;
	}

	try {
		$pdo->query("
		CREATE TABLE IF NOT EXISTS wp_types_definition ( 
			ID INTEGER PRIMARY KEY AUTOINCREMENT,
			info_type INTEGER(9),
			info_name TEXT(255),
			version TEXT(20),
			definition TEXT(1000)
		)");
		$pdo->query("CREATE UNIQUE INDEX IF NOT EXISTS uniq_def ON wp_types_definition (info_type, info_name, definition)");
		$pdo->query("CREATE INDEX IF NOT EXISTS info_name ON wp_types_definition (info_name)");
	} catch(PDOException $e) {
		echo $e->getMessage();
		return false;
	}

	return true;
}